//
//  BulidingInfoLogic.m
//  QRMaster
//
//  Created by myApple on 15/12/22.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "BulidingInfoLogic.h"
#import "OperationDataBaseModel.h"

@interface BulidingInfoLogic()
{
    OperationDataBaseModel * operation;
}
@end

@implementation BulidingInfoLogic

#pragma mark ----------------插入楼栋信息
-(void)insertBulidingInfo:(BulidingModel *)model withCommunityId:(NSString *)communityId
{
    NSMutableDictionary * staffDict = [self modelConvertDict:model];
    [staffDict setValue:communityId forKey:@"communityentity_id"];
    operation = [[OperationDataBaseModel alloc]init];
    [operation insertDataTable:@"buildingInfo" withDict:staffDict];
}


#pragma mark -----------------查询楼栋信息
-(void)queryBulidingInfo:(NSString *)communityId block:(resultBulidingData)bulidingArray
{
    __block NSMutableArray * bulidingMutableArray = [[NSMutableArray alloc]init];
    NSString * condition = [NSString stringWithFormat:@"where %@=%@",@"communityentity_id",communityId];
    operation = [[OperationDataBaseModel alloc]init];
    [operation queryDataFromTable:@"buildingInfo" withCondition:condition block:^(FMResultSet *set) {
        while ([set next]) {
            BulidingModel * model = [[BulidingModel alloc]init];
            model.buildingId = [set stringForColumn:@"buildingid"];
            model.buildingName = [set stringForColumn:@"buildingname"];
            model.buildingNo = [set stringForColumn:@"buildingno"];
            [bulidingMutableArray addObject:model];
        }
        if (bulidingArray) {
            bulidingArray(bulidingMutableArray);
        }
    }];
}

#pragma mark ------楼栋名称
-(void)queryBulidingName:(NSString *)buildingId block:(resultBulidingName)bulidingName
{
    __block NSString * buildNameStr=@"";
        
    NSString * condition = [NSString stringWithFormat:@"where %@=%@",@"buildingId",buildingId];
    operation = [[OperationDataBaseModel alloc]init];
    [operation queryDataFromTable:@"buildingInfo" withCondition:condition block:^(FMResultSet *set) {
        while ([set next]) {
            buildNameStr = [set objectForColumnName:@"buildingname"];
        }
        if (bulidingName) {
            bulidingName(buildNameStr);
        }
    }];
}


#pragma mark ------------- 删除楼栋
-(void)deleteBulidingWithCommunityId:(NSString *)communityId
{
//    NSString * condition = [NSString stringWithFormat:@"where communityentity_id=%@",communityId];
    operation = [[OperationDataBaseModel alloc]init];
    [operation deleteBulidingFromTableByCommunityId:@"buildingInfo" communityId:communityId];
}

/**
 *  模型转化为字典
 *
 *  @param model 组客户模型
 *
 *  @return 可变字典
 */
-(NSMutableDictionary *)modelConvertDict:(BulidingModel *)model
{
    NSMutableDictionary * staffDict = [[NSMutableDictionary alloc]init];
    [staffDict setValue:model.buildingId forKey:@"buildingid"];
    [staffDict setValue:model.buildingName forKey:@"buildingname"];
    [staffDict setValue:model.buildingNo forKey:@"buildingno"];
    return staffDict;
}


@end
